package com.jinlejia.erp.ui.mine.transfer;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import androidx.appcompat.app.AlertDialog;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jinlejia.erp.R;
import com.jinlejia.erp.base.ApiConfig;
import com.jinlejia.erp.base.MapCallBack;
import com.weight.CustomToast;
import com.jinlejia.erp.utils.DataFilter;
import com.zhy.http.okhttp.OkHttpUtils;

import java.util.Map;

import okhttp3.Call;

/**
 * 去审核（Dialog）
 */

public class AuditDialog extends AlertDialog implements View.OnClickListener {
    private Context context;

    public AuditDialog(Context context) {
        super(context);
        this.context = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Window window = this.getWindow();
        // *** 主要就是在这里实现这种效果的.
        // 设置窗口的内容页面,shrew_exit_dialog.xml文件中定义view内容
        window.setContentView(R.layout.dialog_transfer_audit);
        window.setGravity(Gravity.CENTER);
        window.setBackgroundDrawable(new ColorDrawable());
        window.setLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        this.setCanceledOnTouchOutside(false);
        this.setCancelable(false);
        //去审核
        TextView go = (TextView) window.findViewById(R.id.go_audit);
        //已完成
        TextView finish = (TextView) window.findViewById(R.id.finish_audit);

        go.setOnClickListener(this);
        finish.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.go_audit:
                context.startActivity(new Intent(context, TransferAuditListActivity.class));
                break;
            case R.id.finish_audit:
                getData();
//                dismiss();
                break;
            default:
                break;
        }
    }

    private void getData() {
        OkHttpUtils.get()
                .url(ApiConfig.NO_FINISH_AUDIT)
                .build()
                .execute(new MapCallBack() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        e.printStackTrace();
                        call.cancel();
                    }

                    @Override
                    public void onResponse(Map<String, Object> response, int id) {
                        if (DataFilter.filter(context, response)) {
                           if( !(boolean)response.get("data") ){
                               dismiss();
                           }else{
                               CustomToast.showToast(context,"您还有未审核异动",1000);
                           }
                        }
                    }
                });
    }

}
